//
//  DKWBannarTableViewCell.m
//  CreditHub
//
//  Created by chenglang on 2024/9/17.
//

#import "DKWBannarTableViewCell.h"
#import "DKWBannarItemCell.h"
#import "WebViewController.h"
@interface DKWBannarTableViewCell()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@end
@implementation DKWBannarTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    

    [self.collectionView registerNib:[UINib nibWithNibName:@"DKWBannarItemCell" bundle:nil] forCellWithReuseIdentifier:@"DKWBannarItemCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    

    self.pageControl.pageIndicatorTintColor = UIColor.grayColor;
    
}
- (void)setArrayDkwBannar:(NSArray *)arrayDkwBannar{
    _arrayDkwBannar = arrayDkwBannar;
    self.pageControl.numberOfPages = arrayDkwBannar.count;
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arrayDkwBannar.count*300;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DKWBannarItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DKWBannarItemCell" forIndexPath:indexPath];
    NSInteger index = indexPath.row % _arrayDkwBannar.count;
    NSDictionary *dic = self.arrayDkwBannar[index];
    [[ImageLoader sharedInstance] loadImageFromURL:[NSURL URLWithString:dic[@"upset"]] imageView:cell.imgView completion:^(UIImage * _Nonnull image, NSError * _Nonnull error) {
            
    }];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(SCREEN_WIDTH-30, 120);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if( ![UserInfoModel isLogin] ){
        [UserInfoModel login];
        return;
    }
    
    NSInteger index = indexPath.row % _arrayDkwBannar.count;
    NSDictionary *dic = self.arrayDkwBannar[index];
    NSString *url  = dic[@"faint"];
    if ([url length] >= 4) {
        NSString *substring = [url substringToIndex:4];
        if ([substring isEqualToString:@"http"]) {
            WebViewController *webView = [[WebViewController alloc] init];
            webView.url = url;
            [[UIView xhl_getTopViewController].navigationController pushViewController:webView animated:YES];
        } else {
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:^(BOOL success) {
                
            }];
            
        }
    }
    NSLog(@"-------------%lu",index);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger currentPage = (offsetX + self.frame.size.width / 2) / self.frame.size.width;
    if (currentPage == 0) {
//        scrollView.contentOffset = CGPointMake(self.frame.size.width * _imageArray.count, 0);
    } else if (currentPage == _arrayDkwBannar.count * 2 - 1) {
//        scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    self.pageControl.currentPage = currentPage % _arrayDkwBannar.count;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
